<?php

namespace App\Http\Controllers\Admin\V1;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Menu;
use App\Models\Permission;
use App\Traits\ApiResponse;

class MenuController extends Controller
{

    use ApiResponse;

    /**
     * 创建菜单
     * @Author wuyan（466720682@qq.com）
     * @param Request
     * @return [Boolean]
     */
    public function create(Request $request)
    {
        $data = $request->validate([
            'name' => 'required|string',
            'route' => 'required|string',
            'display' => 'integer',
            'sort' => 'integer',
            'icon' => 'string',
            'pid' => 'integer',
        ]);
        (new Menu())->addMenu($data);
        return $this->responseCreated();
    }

    /**
     * 更新菜单
     * @Author wuyan（466720682@qq.com）
     * @param Request
     * @return [Boolean]
     */
    public function update($id, Request $request)
    {
        $data = $request->validate([
            'name' => 'required|string',
            'route' => 'required|string',
            'display' => 'required',
            'sort' => 'required',
            'icon' => 'string',
            'pid' => 'required',
        ]);
        Menu::query()->where('id', $id)->update($data);
        return $this->responseSucceed();
    }

    /**
     * 更新菜单是否显示
     * @param $id
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function isDisplay($id, Request $request)
    {
        $data = $request->validate([
            'display' => 'required',
        ]);
        Menu::query()->where('id', $id)->update($data);
        return $this->responseSucceed();
    }

    /**
     * 删除菜单
     * @Author wuyan（466720682@qq.com）
     * @param Request
     * @return [Boolean]
     */
    public function delete($id)
    {
        if ( Permission::query()->where('menu_id', $id)->first() ) {
            return $this->responseFailed('请先删除菜单下面的权限');
        }
        if ( Menu::query()->where('pid', $id)->first() ) {
            return $this->responseFailed('请先删除下级菜单');
        }
        Menu::query()->where('id', $id)->delete();
        return $this->responseSucceed();
    }

    /**
     * 菜单列表
     * @Author wuyan（466720682@qq.com）
     * @return [Array]
     */
    public function lists()
    {
        $menuModel = new Menu();
        $lists = $menuModel->lists();
        return $this->responseSucceed([
            'data' => $lists,
            'total' => count($lists),
            'success' => true
        ]);
    }

    /**
     * 获取菜单下拉
     * @Author wuyan（466720682@qq.com）
     * @return [Array]
     */
    public function menuSelect()
    {
        $menuModel = new Menu();
        $lists = $menuModel->selecteds();
        return $this->responseSucceed($lists);
    }

    /**
     * 菜单显示列表
     * @Author wuyan（466720682@qq.com）
     * @return [Array]
     */
    public function menuShow()
    {
        $menuModel = new Menu();
        $lists = $menuModel->menuShow();
        return $this->responseSucceed($lists);
    }

}
